function plot2leg_mult(x,in); 
% =====================================================================
% function plot2leg_mult(x,in); 
%
% Input 
% x matrix [T 2 P] 
% Plot each of the 2 columns per page in a subplot using plot YY 
% 
% Options in PNAME 
% 
% in.pnames     (Cell) [NP 1] Names for each of the P pages 
% in.legend     (Cell) [2  1] Legend Cell for each column 
% in.m          Number of Rows per figure 
% in.n          Number of Columns per Figure 
% in.flag_wide  ==1 Orientation Landscape and Wide 
% in.xaxis      Vector of X axis 
% in.axfont     Axis Font 
% in.title      Add to the PNAMES in each title 
% in.supt     (String) Super Title per Page 
% in.titfont    Title Font 
% in.ylabel     [2 1] Cell with Y-Labels gor each  
% in.xlabel     String with X-Label 
% 
% Alejandro Justiniano May 19th 2006 
% ======================================================================
dim=size(x); 
T=dim(1); 
nc=dim(2); 
if length(dim)==2; 
    dim=[dim 1]; 
end 
np=dim(3);  
if nc > 2; error('This is for 2 columns per page, Use PLOTSAJ.M otherwise'); end 
if nargin < 2
    in.pnames = fnumcell('col ',[1:np] ); 
else 
    if isstruct(in) == 0
        error('Input in must be a structure') 
    end 
end
[e,in]=ch_field(in,'flag_wide'); 
if e==0 
    in.flag_wide=0; 
end 
% ================================================
% Check fields and assign default values otherwise
% ================================================
% Subplots per graph page 
% ------------------------
[e1,in]=ch_field(in,'m');
[e2,in]=ch_field(in,'n');
if e1==0 | e2 == 0
    if np > 1
        in.m=2;
        in.n=ceil(np/2) ;
    else
        in.m=1;
        in.n=1;
    end
end
clear e1 e2;
ppg=in.m*in.n;
[e,in]=ch_field(in,'pnames');
if e==0
    in.pnames = fnumcell('col ',[1:np] );
end
[e,in]=ch_field( in, 'legend' );
if e==0
    in.legend={'Ser 1','Ser 2'};
end
[e,in]=ch_field(in,'xlabel');
if e~=0
    flag_xlabel=1;
else
    flag_xlabel=0;
end
[e,in]=ch_field(in,'ylabel');
if e~=0
    flag_ylabel=1;
    if length(in.ylabel)~=2
        error('Need provide one label for each Y axis')
    end
else
    flag_ylabel=0;
end
[e,in]=ch_field(in,'axfont',7);
set(0,'DefaultAxesFontSize',in.axfont);
[e,in]=ch_field(in,'xaxis',[1:T]');
if e~=0
    in.xaxis=in.xaxis(:);
    temp=length(in.xaxis(:) );
    if temp~=T
        error('Length of in.xaxis does not match number of rows in mat')
    end
    clear temp;
end
set(0,'DefaultAxesXLim',[in.xaxis(1) in.xaxis(end)]); 
[e,in]=ch_field(in,'titfont',9);
[e,in]=ch_field(in,'title',''); 
[e,in]=ch_field(in,'supt'); 
if e~=0; 
    flag_supt=1; 
else 
    flag_supt=0; 
end 
ii=1;
ipg=0;
for ii=1:np;
    if ipg==0
        figure;
    end
    ipg=ipg+1;
    subplot(in.m,in.n,ipg);
    [Axhandle,H1,H2]=plotyy(in.xaxis(:),squeeze(x(:,1,ii)),in.xaxis(:),squeeze(x(:,2,ii)));
    title( [in.pnames{ii}, in.title ],'FontSize',in.titfont);
    set(H2,'LineWidth',1.5);
    set(H2,'LineStyle','--');
    if ipg==1
        legend([H1 H2],in.legend); legend boxoff;
        if flag_ylabel==1;
            try
                ylabel(Axhandle(1),char(in.ylabel{1}));
            catch
                try
                    ylabel(Axhandle(1),'Ser 1');
                catch
                    disp('No Y label');
                end
            end
        end
    end
    if ipg==ppg | ii==np; 
        if in.flag_wide==1
            set(gcf,'PaperOrientation','Landscape');
            set(gcf,'PaperPosition',[0.2 1 10.5 7.4]);
        end
        if flag_xlabel==1; 
            xlabel(in.xlabel); 
        end 
        if flag_ylabel==1;
            try
                ylabel(Axhandle(2),char(in.ylabel{2}));
            catch
                try
                    ylabel(Axhandle(2),'Ser 2');
                catch
                    disp('No Y label');
                end
            end
        end 
        ipg=0;
        if flag_supt==1; 
            suptitle_withpatch(char(in.supt)); 
        end 
    end
end
set(0,'DefaultAxesFontSize','remove');
set(0,'DefaultAxesXLim','remove'); 
